package com.szlanyou.thirdsdk.retrofit.client;

import com.szlanyou.thirdsdk.retrofit.https.SSLSocketFactoryUtils;
import com.szlanyou.thirdsdk.retrofit.interceptor.MyLogInterceptor;
import com.szlanyou.thirdsdk.retrofit.interceptor.RequestProgressInterceptor;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;

/**
 * author:@author 陈章 on 2016/6/21 0021.
 * des:
 * 拦截器配置
 */
class RetrofitUtil {
    private final static String HTTPS_HEAD = "https://";

    public static OkHttpClient generateOkHttpClient(HttpClient httpClient) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(10, TimeUnit.SECONDS);
        builder.readTimeout(10, TimeUnit.SECONDS);
        builder.writeTimeout(10, TimeUnit.SECONDS);

        //个性化配置
        httpClient.onCustomSettings(builder);

        //一定要放到后面，否则上传会有问题。
        builder.addInterceptor(new RequestProgressInterceptor());

        //公用的拦截器，不区分HttpClient。(作为最后一个拦截器添加，方便打印前面的拦截器中途都干了啥。)
        builder.addInterceptor(new MyLogInterceptor());

        //https证书
        if (httpClient.getRetrofitClient().getBaseUrl().startsWith(HTTPS_HEAD)) {
            builder.sslSocketFactory(SSLSocketFactoryUtils
                    .createSSLSocketFactory(), SSLSocketFactoryUtils.createTrustAllManager());
        }
        return builder.build();
    }
}
